using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace DotNetCore.MyBlogApi.Api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo
            .Map(evt => evt.Level, (level, wt) =>
            {
                wt.Console();
                wt.File(Path.Combine(AppContext.BaseDirectory, $"Logs/{level.ToString()}/log_{DateTime.Now.ToString("yyyy-MM-dd")}.txt"));
            })
            .CreateLogger();
            try
            {
                Log.Information("Starting up");
                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Application start-up failed");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
           Host.CreateDefaultBuilder(args)
               .UseSerilog()
               .ConfigureWebHostDefaults(webBuilder =>
               {
                   //webBuilder.UseKestrel();
                   webBuilder.UseStartup<Startup>();
                   webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                   //webBuilder.UseIISIntegration();
                   Log.Logger.Information("test");
               });
    }
}
